package com.yao.club.mapper;

import com.yao.club.entity.vo.PostsCommentVO;
import com.yao.club.entity.dto.SysImg;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mapstruct.Mapper;

import java.util.List;

@Mapper
public interface PostsCommentMapper {





    @Insert("insert into posts_comment(posts_id,first_comment_id,parent_id,content,create_by,create_name,head_img_id,level,if_sensitive,create_time,reply_name,be_reply_id) values(#{posts_id},#{first_comment_id},#{parent_id},#{content},#{create_by},#{create_name},#{head_img_id},#{level},#{if_sensitive},now(),#{reply_name},#{be_reply_id})")
    int addComment(@Param("posts_id") String  posts_id, @Param("first_comment_id") String first_comment_id,
               @Param("parent_id") String parent_id, @Param("content") String content,
               @Param("create_by") String create_by, @Param("create_name") String create_name,
               @Param("head_img_id") String head_img_id, @Param("level") int level,
               @Param("if_sensitive") Boolean if_sensitive,
               @Param("reply_name") String reply_name,
               @Param("be_reply_id") String be_reply_id
               );

    /**
     * 根据帖子ID获取一级评论列表
     * @param postsId 帖子ID
     * @return 一级评论列表
     */
    @Select("SELECT * FROM posts_comment WHERE posts_id = #{postsId} AND parent_id = '0' AND del_flag = 0 ORDER BY create_time DESC")
    List<PostsCommentVO> selectFirstLevelComments(@Param("postsId") String postsId);

    /**
     * 根据父评论ID获取二级评论列表
     * @param parentId 父评论ID
     * @return 二级评论列表
     */
    @Select("SELECT * FROM posts_comment WHERE parent_id = #{parentId} AND del_flag = 0 ORDER BY create_time ASC")
    List<PostsCommentVO> selectSecondLevelComments(@Param("parentId") String parentId);

    /**
     * 根据头像ID获取头像信息
     * @param imgId 头像ID
     * @return 头像信息
     */
    @Select("SELECT * FROM sys_img WHERE img_id = #{imgId}")
    SysImg selectHeadImgById(@Param("imgId") String imgId);

    /**
     * 获取帖子的评论总数
     * @param postsId 帖子ID
     * @return 评论总数
     */
    @Select("SELECT COUNT(*) FROM posts_comment WHERE posts_id = #{postsId} AND del_flag = 0")
    int countCommentsByPostsId(@Param("postsId") String postsId);

    /**
     * 帖子点赞数增加
     * @param posts_comment_id
     * @return
     */

    @Update("UPDATE posts_comment SET like_number = like_number + 1 WHERE  posts_comment_id= #{posts_comment_id}")
    int incrementCommentCount(@Param("posts_comment_id") String posts_comment_id);

    /**
     * 帖子点赞数增加
     * @param posts_comment_id
     * @return
     */

    @Update("UPDATE posts_comment SET like_number = like_number - 1 WHERE  posts_comment_id= #{posts_comment_id}")
    int unincrementCommentCount(@Param("posts_comment_id") String posts_comment_id);

    /**
     * 删除帖子
     * @param posts_comment_id
     * @return
     */

    @Update("delete from  posts_comment  WHERE  posts_comment_id= #{posts_comment_id} and create_by=#{create_by}")
    int deleteComment(@Param("posts_comment_id") String posts_comment_id, @Param("create_by") String create_by);

}
